<?php

namespace Modules\Admin\Http\Controllers\ActivationCodes;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\ActivationCodeUsage;
use Modules\Common\Models\ActivationCodeLogs;
use Modules\Common\Models\ActivationCodes;
use Modules\Common\Models\SubscriptionRecords;
use Modules\Common\Util\Api;
use Modules\Common\Models\Helpers\TrashManager;
use Modules\Common\Util\Database;
use Modules\Common\Exceptions\Base\Exception;


class Delete
{
     /**
     * @Route(path=/api/admin/activation-code  method=delete)
     */
    public function index()
    {

        DB::transaction(function () {

            $activationCodes = ActivationCodes::find(request('id'));
            if ($activationCodes == null) {
                Exception::new()
                    ->tellUser('激活码不存在')
                    ->logNotice('激活码不存在', ['request' => request()->all()])
                    ->throw();
            }
            //开始记录删除操作，添加至回收站
            TrashManager::startTrace(sprintf('删除激活码：%s', $activationCodes->code));


            $activationCodeUsage = ActivationCodeUsage::where('code', 'like', '%' . $activationCodes->code . '%')->count();

            if ($activationCodeUsage > 0) {
                Exception::new()
                    ->tellUser('激活码存在关联记录，不可删除')
                    ->logNotice('激活码存在关联记录，不可删除', ['request' => request()->all()])
                    ->throw();
            }

            // 删除操作
            $activationCodes->delete();

            TrashManager::endTrace();
        });


        return Api::success();
    }
    
    
     /**
     * @Route(path=/api/admin/activation-code-batch-delete  method=delete)
     */
    public function index2()
    {
        // 假设请求中传递了一个包含多个分类 ID 的数组，例如：ids=1,2,3
        $activationCodesIds = request('ids');
        
        // 将字符串转换为数组
        $activationCodesIdsArray = explode(',', $activationCodesIds);
        
        // 启动数据库事务
        DB::transaction(function () use ($activationCodesIdsArray) {
            foreach ($activationCodesIdsArray as $activationCodeId) {
                $activationCodes = ActivationCodes::find($activationCodeId);
                if ($activationCodes == null) {
                    Exception::new()
                        ->tellUser('激活码不存在')
                        ->logNotice('激活码不存在', ['request' => request()->all()])
                        ->throw();
                }
                // 开始记录删除操作，添加至回收站
                TrashManager::startTrace(sprintf('删除激活码：%s', $activationCodes->code));
                
               $activationCodeUsage = ActivationCodeUsage::where('code', 'like', '%' . $activationCodes->code . '%')->count();
    
                if ($activationCodeUsage > 0) {
                    Exception::new()
                        ->tellUser('存在激活码关联记录，不可删除')
                        ->logNotice('存在激活码关联记录，不可删除', ['request' => request()->all()])
                        ->throw();
                }
    
                // 删除操作
                $activationCodes->delete();
    
                TrashManager::endTrace();
            }
        });


        return Api::success();
    }

}